/**
 * Created by yanghaibo
 */
var activityDetailParam = {

    pieOption: {
        tooltip: {
            trigger: 'item',
            formatter: function() {
                return '';
            }
        },
        legend: {
            orient: 'vertical',
            selectedMode: false,
            align: 'left',
            x: 'right',
            // left: 'right',
            // top:'middle',
            data:['发券数','核券数']
        },
        series: [
            {
                name:'核销率',
                type:'pie',
                radius: ['50%', '70%'],
                avoidLabelOverlap: false,
                legendHoverLink: false,
                hoverAnimation: false,
                selectedMode: false,
                label: {
                    normal: {
                        show: true,
                        position: 'center',
                        formatter: function(p) {
                            if (p.name.indexOf('发券数') === 0)
                                return "";
                            return "核销率\n" + p.percent + '%';
                        },
                        textStyle: {
                            fontSize: '20',
                            color: '#000000',
                            fontWeight: 'bold'
                        }
                    }
                },

                data:[{
                    name: "发券数",
                    itemStyle: {
                        normal: {
                            color: "#508cee"
                        }
                    }
                },{
                    name: "核券数",
                    itemStyle: {
                        normal: {
                            color: "#96baf5"
                        }
                    }
                }]
            }
        ]
    },

    lineOption: {

        tooltip : {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#6a7985'
                }
            }
        },
        legend: {data: []},
        grid: {

            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis : [
            {
                type : 'category',
                boundaryGap : false,
                data : []
            }
        ],
        yAxis : [
            {
                type : 'value',
                name: "",
                nameTextStyle: {
                    color: "#508cee"
                }
            }
        ],
        series : [
            {
                name:'邮件营销',
                type:'line',
                data:[120, 132, 101, 134, 90, 230, 210]
            }
        ]
    }

};
(function(doc, win) {

    // sui 页面就绪时间
    $(doc).on("pageInit", "#activityDetailPage", function(e, pageId, $page, isBack) {
        if (isBack)
            return;
        var param = CommonUtil.takeOutParamObject();
        var controller = new ActivityDetailController();
        controller.getActivityDetail(param.campId, param.outBizNo);
        if (param.activityStatus != -2 && param.activityStatus != -1) {
            controller.queryActivityEffectAnalsysiData(param.campId, param.outBizNo);
            $("#activityDetailPage .tab-wrap").css("display", "");
            CommonUtil.showElement("#activityDetailPage .effect-analysis");
            CommonUtil.hideElement("#activityDetailPage .details-page");
            $("#activityDetailPage .tab-wrap>li").eq(0).addClass("selected").siblings("li").removeClass("selected");
        } else {
            // 创建失败活动，隐藏效果分析
            CommonUtil.hideElement("#activityDetailPage .tab-wrap");
            CommonUtil.hideElement("#activityDetailPage .effect-analysis");
            CommonUtil.showElement("#activityDetailPage .details-page");
        }

    });

    //活动效果和活动详情切换
    $(doc).on("click", "#activityDetailPage .tab-wrap>li", function () {
        if (activityDetailParam.loading) {
            return;
        }
        var $this = $(this);
        var tab = $this.parent("ul");
        $this.addClass("selected").siblings("li").removeClass("selected");
        tab.nextAll().css("display", "none").eq($this.index()).css("display", "block");
    });

    // 终止活动
    $(doc).on("click", "#activityDetailPage .header .right a", function () {
        DialogUtil.dialogConfirm({
            title: "确定要终止活动吗？",
            content: "<center>活动终止后，优惠券将不再继续发放，但已发出的券依然有效</center>",
            btnLeft: "取消",
            btnRight: "终止",
            callbackRight: function () {
                var controller = new ActivityDetailController();
                controller.stopActivity(activityDetailParam.activityVo.campId, activityDetailParam.activityVo.outBizNo);
            }
        });
    });

    // 点击优惠券，弹窗
    $(doc).on("click", "#activityDetailPage .quan-wrap > li", function () {
        var $this = $(this);
        var promoToolId = $this.attr("promoToolId");
        var promoTool;
        for (var i = 0; i < activityDetailParam.activityVo.promTools.length; i++) {
            var item = activityDetailParam.activityVo.promTools[i];
            if (item.promoToolId == promoToolId) {
                promoTool = item;
                break;
            }
        }
        if (!promoTool) {
            return;
        }
        var dialogDatas = [];
        var ticketDate;
        if (promoTool.voucherValidateType == 'FIXED') {
            ticketDate = promoTool.voucherStartTimeStr + "~" + promoTool.voucherEndTimeStr;
        } else {
            ticketDate = "领取后" + promoTool.voucherRelativeTime + "天内有效";
        }
        if (activityDetailParam.activityVo.activityMarketType == 22 ||
            activityDetailParam.activityVo.activityMarketType == 35 ||
            activityDetailParam.activityVo.activityMarketType == 51) {
            // 消费送券
            dialogDatas.push({"title": "领取条件", "info": "订单满 " + promoTool.promoSendRuleMincost + "元"});
            if (promoTool.voucherType == "MONEY") {
                dialogDatas.push({"title": "券面额", "info": promoTool.voucherWorthValue + "元"});
            } else {
                dialogDatas.push({"title": "券折扣", "info": promoTool.voucherRate * 10 + "折"});
            }

            dialogDatas.push({"title": "券消费门槛", "info": "订单满 " + promoTool.voucherMinConsume + "元"});
            dialogDatas.push({"title": "券有效期", "info": ticketDate});
            dialogDatas.push({"title": "券名称", "info": promoTool.voucherName});
            if (promoTool.voucherImgUrl) {
                dialogDatas.push({"title": "券图片", "src": promoTool.voucherImgUrl});
            } else {
                dialogDatas.push({"title": "券图片", "info": "--"});
            }
        } else if (activityDetailParam.activityVo.activityMarketType == 47) {
            // 单品营销
            var goodsPic = "";
            if (promoTool.voucherItem.itemImgs) {
                goodsPic = JSON.parse(promoTool.voucherItem.itemImgs)[0].url;
            }
            dialogDatas.push({"title": "商品名称", "info": promoTool.voucherItem.itemName});
            if (goodsPic) {
                dialogDatas.push({"title": "商品图片", "src": goodsPic});
            } else {
                dialogDatas.push({"title": "商品图片", "info": "--"});
            }
            dialogDatas.push({"title": "券面额", "info": promoTool.voucherWorthValue + "元"});
            dialogDatas.push({"title": "券消费门槛", "info": "订单满 " + promoTool.voucherMinConsume + "元"});
            dialogDatas.push({"title": "券有效期", "info": ticketDate});
        } else {
            dialogDatas.push({"title": "券面额", "info": promoTool.voucherWorthValue + "元"});
            dialogDatas.push({"title": "券消费门槛", "info": "订单满 " + promoTool.voucherMinConsume + "元"});
            dialogDatas.push({"title": "券有效期", "info": ticketDate});
        }
        couponSetDialog({data: dialogDatas});
    });

    // 投放渠道弹窗
    $(doc).on("click", "#activityDetailPage .qudao-list li", function () {
        var publishChannelType = $(this).attr("publishChannelType");
        if (publishChannelType == "SHOP_DETAIL") {
            // 支付宝
            var dialogDatas = [];
            var voucherSlogan = activityDetailParam.activityVo.promTools[0].voucherSlogan;
            var voucherSloganUrl = activityDetailParam.activityVo.promTools[0].voucherSloganUrl;
            dialogDatas.push({"title": "专属优惠文案", "info": voucherSlogan || "--"});
            if (voucherSloganUrl) {
                dialogDatas.push({"title": "专属优惠图片", "src": voucherSloganUrl});
            } else {
                dialogDatas.push({"title": "专属优惠图片", "info": "--"});
            }
            couponSetDialog({data: dialogDatas, title: "支付宝"});
        } else if (publishChannelType == "A_PAY_CODE") {
            // 收银码
        } else if (publishChannelType == "COMMISSION_SETTING") {
            // 口碑客
            var dialogDatas = [];
            var commissionSettingModel = null;
            for (var i = 0; i < activityDetailParam.activityVo.publishChannels.length; i++) {
                if (activityDetailParam.activityVo.publishChannels[i].publishChannelType === "COMMISSION_SETTING") {
                    commissionSettingModel = activityDetailParam.activityVo.publishChannels[i].commissionSettingModel;
                    break;
                }
            }
            if (commissionSettingModel.clauseType == "QUOTA_CLAUSE") {
                dialogDatas.push({"title": "分佣金额", "info": commissionSettingModel.quotaClauseQuotaAmount + "元"});
            } else if (commissionSettingModel.clauseType == "PERCENTAGE_CLAUSE") {
                dialogDatas.push({"title": "分佣比例", "info": commissionSettingModel.percentageClauseRate + "%"});
                dialogDatas.push({"title": "分佣上限", "info": commissionSettingModel.percentageClauseMax ? (commissionSettingModel.percentageClauseMax + "元") : "不限制"});
            } else {
                // TODO MISSION_CLAIM_CLAUSE:专属认领人条款
            }
            couponSetDialog({data: dialogDatas, title: "口碑客"});
        } else {
            return;
        }
    });


    // roi解释
    $(doc).on("click", "#activityDetailPage .roi .zhk-list_analyze", function () {
        var data = {
            title:"ROI名词说明",
            subTitle:"ROI：1块钱成本带来的活动收益",
            infoList:["ROI高于6：表明营销活动成功，高于同行业90%的商家",
                "ROI介于3~6：综合店内因素，加大营销活动力度",
                "ROI低于3：建议您针对活动人群创建更合理的营销活动"]};
        explanationDialog({data: data});
    });



    //活动其他设置
    $(doc).on("click", "#activityDetailPage a.other-setting", function () {
        var param = activityDetailParam.activityVo.promTools[0];
        CommonUtil.injectParamObject(param);
        $.router.load("activity_details_other_setting.html");
    });

    // 活动分析时间
    $(doc).on("click", "#activityDetailPage .filter-tab li", function () {
        var $this = $(this);
        if ($this.hasClass("selected")) {
            return;
        }
        $this.addClass("selected").siblings().removeClass("selected");
        var days = $this.attr("days");
        var controller = new ActivityDetailController();
        if ($this.parent().siblings(".chart-container").attr("id") === "detailPageLineChart") {
            controller.queryActivityOtherTrendData(
                activityDetailParam.activityVo.campId,
                activityDetailParam.activityVo.outBizNo,
                activityDetailParam.activityVo.activityMarketType,
                days
            );
        } else {
            controller.queryActivityIncomeTrendData(
                activityDetailParam.activityVo.campId,
                activityDetailParam.activityVo.outBizNo,
                days
            );
        }
    });

})(document, window);

